package com.oscar.ehome.utils.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class VersionUtil {
    public static String getVersionName(Context context) {
        PackageManager manager = context.getPackageManager();
        String packageName = context.getPackageName();
        int flag = 0;

        try {
            PackageInfo packageInfo = manager.getPackageInfo(packageName, flag);
            return packageInfo.versionName;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static int getVersionCode(Context context) {
        //工具PackageManager:解析功能清单的xml工具
        PackageManager pm = context.getPackageManager();
        //获取版本
        String packageName = context.getPackageName();
        int flags = 0;//默认值
        try {
            //PackageInfo:javaBean对象存解析出来的所有变量
            PackageInfo packageInfo = pm.getPackageInfo(packageName, flags);
            //获取版本号:是一个自然数,每一次更新都+1
            return packageInfo.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }

}
